*** This performes balancing tests control and treatment group at baseline

if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}

**** Table A.2 – Balancing original control and treatment group at baseline **************
use "${gsdAnalysisOutput}/bscie_panel_regressions", clear

keep if wave==1 


* Individual level characteristics

clear matrix
mat A = J(37,6,.)
mat stars = J(37,6,0)

local n 1

quietly tabulate edu_level, gen(edu_level_)
quietly tabulate lit_eval, gen(lit_eval_)
quietly tabulate num_eval, gen(num_eval_)
quietly tabulate state_assigned, gen(cat_state_assigned)

foreach var in age gender married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
edu_level_1  edu_level_2 edu_level_3 edu_level_4 lit_eval_1 lit_eval_2 lit_eval_3  ///
num_eval_1 num_eval_2 num_eval_3  num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist death_x_prox_b300 death_x_prox_a300  {


ttest `var', by(treatment)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if treatment == 0
local N=`r(N)'
mat A[30,1]=`N'
display `N'
sum `var' if treatment == 1
local N=`r(N)'
mat A[30,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}

********************************************************************************

reg treatment age gender  married  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_a300 death_x_prox_b300  

test age  married gender  biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_a300 death_x_prox_b300

mat A[29,5] = `r(F)'
mat A[29,6] = `r(p)'
matrix stars[30,5] = (abs(A[31,5]/A[31,6]) > invttail(r(df_t),0.10/2)) + (abs(A[31,5]/A[31,6]) > invttail(r(df_t),0.05/2)) + (abs(A[31,5]/A[31,6]) > invttail(r(df_t),0.01/2))

local n 30

foreach var in cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 {

ttest `var' , by(treatment)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if  treatment == 0
local N=`r(N)'
mat A[36,1]=`N'
display `N'
sum `var' if  treatment == 1
local N=`r(N)'
mat A[36,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}


reg treatment cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6  

test cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 

mat A[37,5] = `r(F)'
mat A[37,6] = `r(p)'

matrix stars[37,5] = (abs(A[37,5]/A[37,6]) > invttail(r(df_t),0.10/2)) + (abs(A[37,5]/A[37,6]) > invttail(r(df_t),0.05/2)) + (abs(A[37,5]/A[37,6]) > invttail(r(df_t),0.01/2))


*************************
* Make the table

matrix rownames A = "Age" "Female" "Married" "Business ownership" "(Log) food consumption food"  ///
"Formal bank account" "Formal loans in past 5 years" "Informal loans in past 5 years" "Education level: No education"  "Education level: Some primary" "Education level: Some secondary" "Education level: Some university" ///
"Literacy: No English" "Literacy: Some English" "Literacy: Good English"   "Numeracy: Low"  "Numeracy: Counting 0-100" "Numeracy: Addition" "Numeracy: Multiplication" ///
 "Household size" "Number of children" "Number of elderly" "Number of rooms" "Number of buildings" "(Log) distance to KCB branch" ///
  "Conflict exposure (2011-2014)" "Conflict exposure (2015-2017)" "Observations" "Joint orthogonality" "Central Equatoria" "Eastern Equatoria" "Lakes" "Northern Bahr El Ghazal" "Western Bahr El Ghazal" "Western Equatoria" "Observations" "Joint orthogonality"
frmttable, statmat(A) replace annotate(stars) asymbol(*,**,***) sdec(3) title(Table A.2 – Balancing original control and treatment group at baseline) ctitles("", "", "Control - mean", "Control - SD",  "T - mean", "T - SD", "diff", "SE" ) 
frmttable using "$gsdTables/TableA.2.tex", replay tex replace plain

